package cn.edu.xmu.oomall.core.util;

import cn.edu.xmu.oomall.core.model.PageVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author wangzhongyu
 * @date 2021-12-18
 */
public class MicroCommon {
    public static <T> T getSimpleData(String object, Class<T> classz) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();

            JsonNode root = objectMapper.readTree(object);
            String dataJson = root.get("data").toString();
            T data = objectMapper.readValue(dataJson, classz);
            return data;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> PageVo<T> getPageData(String object, Class<T> classz) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();

            JsonNode root = objectMapper.readTree(object);
            String dataJson = root.get("data").toString();
            PageVo<T> data = objectMapper.readValue(dataJson, new TypeReference<PageVo<T>>() {});
            return data;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
